import React, {useContext, useLayoutEffect, useEffect} from 'react';
import UedTheme from 'hzero-front/lib/components/UedTheme'
import Container from '@hzero-front-ui/cfg/lib/components/Container';
import useThemeHelper from '@hzero-front-ui/cfg/lib/components/Container/useThemeHelper';
import ThemeContext from '@hzero-front-ui/cfg/lib/utils/ThemeContext'
import {defaultConfig} from '@hzero-front-ui/cfg/lib/utils/config'

const BasicContainer: React.FC = (props) => {
  const context = useContext(ThemeContext);
  const { setLocalTheme } = useThemeHelper();
  useLayoutEffect(() => {
    console.log(context);
    if (location.search) {
      const theme = location.search.split('=')[1];
      const conf = {
        current: {
          ...defaultConfig,
          schema: theme,
        },
        prev: {},
      }
      // @ts-ignore
      context.setTheme(conf)
      setLocalTheme(conf)
    }
  }, [])
  return (
    <>
      {props.children}
    </>
  );
}

export default function withUed(WrappedComponent: any) {
  return (props: any) => {
    return (
      <Container defaultTheme="theme1">
        <BasicContainer>
          <UedTheme />
          <WrappedComponent {...props} />
        </BasicContainer>
      </Container>
    )
  }
}